﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Hr.IBLL;
using Newtonsoft.Json;
using Hr.BLLContainer;
using System.Data.Entity;
using Newtonsoft.Json.Linq;
using Hr.BLL;
using Hr.Model;
using System.Web;
using HR.HrHelper;
using Microsoft.Ajax.Utilities;
using System.Web.Helpers;

namespace HR.Areas.liujunjie.Controllers.Task
{
    public class TaskMangeController : ApiController
    {
        //全局变量，必须的
        //导入BLL层
        protected TaskService Task = new TaskService();

        int code = 0;
        string msg = "success";

        /// <summary>
        /// 获取当前登录用户的任务管理页面信息
        /// </summary>
        /// <param name="id">当前登录的用户id</param>
        /// <returns>任务列表</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/8</create>
        /// <updata>null</updata>

        [HttpGet]
        public JObject SendTaskList(int page, int limit, int UserId)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Task.SendTaskList(page,limit,UserId) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 根据UserList查询用户姓名FullName
        /// </summary>
        /// <param name="id">FullName</param>
        /// <returns>任务列表</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/13</create>
        /// <updata>null</updata>

        [HttpGet]
        public object ReceiveUserTextBinding(string UserList)
        {
            System.Collections.Specialized.NameValueCollection data = HttpContext.Current.Request.Form;

            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Task.ReceiveUserTextBinding(UserList)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 根据Title查询数据
        /// </summary>
        /// <param name="id">Title</param>
        /// <returns>任务列表</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/9</create>
        /// <updata>null</updata> 

        [HttpGet]
        public JObject TitleSelectTaskList(int page, int limit, int UserId, string Title)
        {

            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Task.TitleSelectTaskList(page,limit,UserId,Title) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 下拉框绑定
        /// </summary>
        /// <param name="id">下拉框</param>
        /// <returns>任务列表</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/9</create>
        /// <updata>null</updata>

        [HttpGet]

        public JObject SelectBinding()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg","1" },
                {"TypeIDdata",Task.SelectTypeBinding() },
                {"StatusIDdata",Task.SelectStatusBinding() }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 根据pk_Task查询需要修改的数据
        /// </summary>
        /// <param name="id">Task_List</param>
        /// <returns>任务列表</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/9</create>
        /// <updata>null</updata>

        [HttpGet]
        public JObject EditSelectBinding(int pk_Task)
        {

            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Task.EditSelectBinding(pk_Task) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 根据pk_Task查询需要修改的数据 查看页面
        /// </summary>
        /// <param name="id">Task_List</param>
        /// <returns>任务列表</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/20</create>
        /// <updata>null</updata>

        [HttpGet]
        public JObject ViewSelectBinding(int pk_Task)
        {

            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Task.ViewSelectBinding(pk_Task) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 给Task_List表添加或者修改数据
        /// </summary>
        /// <param name="id">Task_List</param>
        /// <returns>任务列表</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/9</create>
        /// <updata>null</updata>

        [System.Web.Mvc.ValidateInput(false)]
        [HttpPost]
        public JObject EditTaskLsit()
        {
            System.Collections.Specialized.NameValueCollection data = HttpContext.Current.Request.Form;
            //调用jsonhelper帮助类转换成对象
            Task_List tl = JsonHelper.DeserializeJsonToObject<Task_List>(data["Task"]) as Task_List;

            int pk_User = int.Parse(data["UserID"].ToString());
            string sumUserFeedback = data["sumUserFeedback"];
            string CheckDt = "";
            if (data["User"] != null)
            {
                CheckDt = string.Join("|", data["User"].Split(','));
            }

            //创建日期
            //tl.CreateDate = DateTime.Now;

            string type = data["Type"];

            if (type.Equals("Updata"))
            {
                Task_List SelTl = Task.GetModels(n => n.pk_Task == tl.pk_Task).FirstOrDefault();
                msg = "更新成功";
                if (SelTl == null)
                {
                    code = 1;
                    msg = "角色不存在";
                }
                else
                {
                    if (ModelHelper.ReflectModel(tl, SelTl))
                    {
                        msg = "无任何更新";
                    }
                    else
                    {
                        if (!Task.EditTaskList(tl, type, CheckDt, pk_User))
                        {
                            code = 1;
                            msg = "更新失败";
                        }
                    }
                }
            }
            else
            {
                msg = "添加成功";

                if (!Task.EditTaskList(tl, type, CheckDt, pk_User))
                {
                    code = 1;
                    msg = "添加失败";
                }
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Task.GetModels(n=>true)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 修改保存
        /// </summary>
        /// <param name="id">pk_Task</param>
        /// <returns>查看页面保存</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/21</create>
        /// <updata>null</updata>

        [System.Web.Mvc.ValidateInput(false)]
        [HttpPost]
        public JObject TaskViewUpdata()
        {

            System.Collections.Specialized.NameValueCollection data = HttpContext.Current.Request.Form;
            //调用jsonhelper帮助类转换成对象
            Task_List tl = JsonHelper.DeserializeJsonToObject<Task_List>(data["Task"]) as Task_List;

            string sumUserFeedback = data["sumUserFeedback"];
            Task_List SelTl = Task.GetModels(n => n.pk_Task == tl.pk_Task).FirstOrDefault();
            msg = "更新成功";
            if (SelTl == null)
            {
                code = 1;
                msg = "角色不存在";
            }
            else
            {
                if (tl.Equals(SelTl))
                {
                    msg = "无任何更新";
                }
                else
                {
                    if (!Task.TaskViewUpdata(tl, sumUserFeedback))
                    {
                        code = 1;
                        msg = "更新失败";
                    }
                }
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",Task.GetModels(n=>true)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 根据pk_Task删除对应的数据
        /// </summary>
        /// <param name="id">当前登录的用户id</param>
        /// <returns>任务列表删除</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/9</create>
        /// <updata>null</updata>

        [HttpGet]
        public JObject DelTaskList(int pk_Task)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg","1" },
                {"data",Task.Delete(Task.GetModels(n=>n.pk_Task == pk_Task).FirstOrDefault()) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 获取当前登录用户接收的任务页面信息
        /// </summary>
        /// <param name="id">当前登录的用户id</param>
        /// <returns>任务列表</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/849</create>
        /// <updata>null</updata>

        [HttpGet]
        public JObject ReceiveTaskList(int page, int limit, string UserId)
        {

            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Task.ReceiveTaskList(page,limit,UserId) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 根据Title查询数据
        /// </summary>
        /// <param name="id">Title</param>
        /// <returns>任务列表</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/11</create>
        /// <updata>null</updata> 

        [HttpGet]
        public JObject TitleSelectMyTaskList(int page, int limit, string UserId, string Title)
        {

            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Task.TitleSelectMyTaskList(page,limit,UserId,Title) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 根据pk_Task查询数据进入查看页面
        /// </summary>
        /// <param name="id">pk_Task</param>
        /// <returns>任务列表</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/9</create>
        /// <updata>null</updata>

        [HttpGet]
        public JObject PkTaskSelectTaskList(int pk_Task)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Task.GetModels(n=>n.pk_Task==pk_Task).FirstOrDefault() }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 查询接收人 查询用户表
        /// </summary>
        /// <param name="id"></param>
        /// <returns>新增接收人</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/11</create>
        /// <updata>null</updata>

        [HttpGet]
        public JObject UserList(int page, int limit)
        {

            //获取头像数据
            //List<object> UL = new List<object>();
            //foreach (var item in Task.UserList(page, limit))
            //{
            //    string path = item.GetType().GetProperty("ImagePath1").GetValue(item).ToString();
            //    var it = new
            //    {
            //        item,
            //        ImagePath1 = FileHelper.GetImgInfo(path)
            //    };
            //    UL.Add(it);
            //}

            Dictionary<string, object> dt = new Dictionary<string, object>() {
                {"code",code },
                {"msg",msg },
                {"data",Task.UserList(page, limit) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        [HttpGet]
        public JObject UserSelectList(int page, int limit, string UserSelect)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>() {
                {"code",code },
                {"msg",msg },
                {"data",Task.UserSelectList(page,limit,UserSelect) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
    }
}
